package com.dwei;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Element;

import com.dwei.entity.Entity;

public class MyConfiguration {
//============================================================================
//parameter
//============================================================================
	/** 文件编码 */
	private String encoding;
	/** 工程源文件路径（末尾无斜杠） */
	private String source;
	private String packageEntity;
	private String packageDao;
	private String packageService;
	private String packageServiceImp;
	private String packageMapper;
	private String packageInjection;
	/** 所有实体类 */
	private List<Entity> entities = new ArrayList<Entity>();
//============================================================================
//constructor
//============================================================================
	public MyConfiguration(Element configuration) {
		Element common = configuration.element(Global.CONF_COMMON);
		setEncoding(common.elementText(Global.COMMON_ENCODING));
		setSource(common.elementText(Global.COMMON_SOURCE));
		setPackageEntity(common.elementText(Global.COMMON_ENTITY));
		setPackageDao(common.elementText(Global.COMMON_DAO));
		setPackageService(common.elementText(Global.COMMON_SERVICE));
		setPackageServiceImp(common.elementText(Global.COMMON_SERVICE_IMP));
		setPackageMapper(common.elementText(Global.COMMON_MAPPER));
		setPackageInjection(common.elementText(Global.COMMON_INJECTION));
		
		Iterator<Element> itrtEntities = configuration.element(Global.CONF_ENTITIES).elementIterator(Global.ENTITY);
		Entity entity;
		String namePre = getPackageEntity() + ".";
		if (namePre.equalsIgnoreCase(".")) namePre = "";
		while (itrtEntities.hasNext()) {
			entity = new Entity(itrtEntities.next());
			entity.setNameMapper(namePre + entity.getNameEntity() + "." + Global.CONFIG_MAPPER_SUF);
			entity.setNameInjection(namePre + entity.getNameEntity() + "." + Global.CONFIG_SERVICE_SUF);
			getEntities().add(entity);
		}
	}
//============================================================================
//public
//============================================================================
	public Map<String, Object> getEntityObjs(Entity entity) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("packageEntity", getPackageEntity());
		result.put("nameEntity", entity.getNameEntity());
		result.put("imports", entity.getImports());
		result.put("properties", entity.getProperties());
		return result;
	}
	public Map<String, Object> getMapObjs(Entity entity) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("encoding", getEncoding());
		result.put("packageEntity", getPackageEntity());
		result.put("nameEntity", entity.getNameEntity());
		result.put("packageDao", getPackageDao());
		result.put("nameDao", entity.getNameDao());
		result.put("properties", entity.getProperties());
		result.put("table", entity.getTable());
		result.put("keyproperty", entity.getColPropMap().get(entity.getKey()));
		result.put("insert", entity.isInsert());
		result.put("update", entity.isUpdate());
		result.put("delete", entity.isDelete());
		return result;
	}
	public Map<String, Object> getInjectionObjs(Entity entity) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("encoding", getEncoding());
		result.put("packageDao", getPackageDao());
		result.put("nameDao", entity.getNameDao());
		result.put("packageServiceImp", getPackageServiceImp());
		result.put("nameServiceImp", entity.getNameServiceImp());
		return result;
	}
	public Map<String, Object> getDaoObjs(Entity entity) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("packageEntity", getPackageEntity());
		result.put("nameEntity", entity.getNameEntity());
		result.put("packageDao", getPackageDao());
		result.put("nameDao", entity.getNameDao());
		result.put("keyproperty", entity.getColPropMap().get(entity.getKey()));
		result.put("insert", entity.isInsert());
		result.put("update", entity.isUpdate());
		result.put("delete", entity.isDelete());
		return result;
	}
	public Map<String, Object> getServiceObjs(Entity entity) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("packageEntity", getPackageEntity());
		result.put("nameEntity", entity.getNameEntity());
		result.put("packageService", getPackageService());
		result.put("nameService", entity.getNameService());
		result.put("keyproperty", entity.getColPropMap().get(entity.getKey()));
		result.put("insert", entity.isInsert());
		result.put("update", entity.isUpdate());
		result.put("delete", entity.isDelete());
		return result;
	}
	public Map<String, Object> getServiceImpObjs(Entity entity) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("packageEntity", getPackageEntity());
		result.put("nameEntity", entity.getNameEntity());
		result.put("packageDao", getPackageDao());
		result.put("nameDao", entity.getNameDao());
		result.put("packageService", getPackageService());
		result.put("nameService", entity.getNameService());
		result.put("packageServiceImp", getPackageServiceImp());
		result.put("nameServiceImp", entity.getNameServiceImp());
		result.put("keyproperty", entity.getColPropMap().get(entity.getKey()));
		result.put("insert", entity.isInsert());
		result.put("update", entity.isUpdate());
		result.put("delete", entity.isDelete());
		return result;
	}
//============================================================================
//override
//============================================================================
	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("编码:").append(getEncoding())
		.append(",\n源码路径:").append(getSource())
		.append(",\nentity包路径:").append(getPackageEntity())
		.append(",\ndao包路径:").append(getPackageDao())
		.append(",\nservice包路径:").append(getPackageService())
		.append(",\nserviceImp包路径:").append(getPackageServiceImp())
		.append(",\nmapper包路径:").append(getPackageMapper())
		.append(",\ninjection包路径:").append(getPackageInjection())
		.append(",\n========================================================")
		.append("\nEntity:\n");
		Iterator<Entity> itrtEntities = getEntities().iterator();
		while (itrtEntities.hasNext()) {
			sb.append(itrtEntities.next().toString()).append("\n");
		}
		sb.append("========================================================");
		return sb.toString();
	}
//============================================================================
//getter/setter
//============================================================================
	public String getEncoding() {
		return encoding;
	}
	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}
	public String getSource() {
		return source;
	}
	public void setSource(String source) {
		this.source = source;
	}
	public String getPackageEntity() {
		return packageEntity;
	}
	public void setPackageEntity(String packageEntity) {
		this.packageEntity = packageEntity;
	}
	public String getPackageDao() {
		return packageDao;
	}
	public void setPackageDao(String packageDao) {
		this.packageDao = packageDao;
	}
	public String getPackageService() {
		return packageService;
	}
	public void setPackageService(String packageService) {
		this.packageService = packageService;
	}
	public String getPackageServiceImp() {
		return packageServiceImp;
	}
	public void setPackageServiceImp(String packageServiceImp) {
		this.packageServiceImp = packageServiceImp;
	}
	public String getPackageMapper() {
		return packageMapper;
	}
	public void setPackageMapper(String packageMapper) {
		this.packageMapper = packageMapper;
	}
	public String getPackageInjection() {
		return packageInjection;
	}
	public void setPackageInjection(String packageInjection) {
		this.packageInjection = packageInjection;
	}
	public List<Entity> getEntities() {
		return entities;
	}
	public void setEntities(List<Entity> entities) {
		this.entities = entities;
	}
}
